/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class code4 {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode dummy = new ListNode(0,head);//傀儡节点，避免left=head这种情况
        ListNode cur = dummy;
        for(int i=0;i<left-1;i++) {
            cur = cur.next;
        }
        //cur走到left-1位置
        ListNode prev = null;
        ListNode Ncur = cur.next;
        for(int i=0;i<right-left+1;i++) {
            ListNode curNext = Ncur.next;
            Ncur.next = prev;
            prev = Ncur;
            Ncur = curNext;
        }
        //cur.next 为反转前的第一个元素
        cur.next.next = Ncur;
        cur.next = prev;

        return dummy.next;
    }
}